﻿/*
=======================================================
    Role【用户角色】
=======================================================
*/

using Entities;

namespace Services
{
    public interface IRoleRepository : IRepository<Role>
    {
        int UpdateIsDelete(bool flag, int[] ids);
        (bool, List<int>) GetCurrentUserRoleIDsByTypeWithDepartment();
    }


    public class RoleRepository : BaseRepository<Role>, IRoleRepository
    {
        public RoleRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<Role> query)
            : base(dB, currentUser, query)
        {
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, int[] ids)
        {
            return _DB.Updateable<Role>()
                      .SetColumns(x => new Role { IsDelete = flag })
                      .Where(x => ids.Contains(x.RoleID))
                      .ExecuteCommand();
        }
        #endregion

        #region Insert
        public override int Insert(Role entity)
        {
            var roleID = base.Insert(entity);

            if (entity.PermissionIDs.IsNotEmpty() && entity.PermissionIDs.Count > 0)
            {
                var rolePermissions = entity.PermissionIDs.Select(x => new RolePermission
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    PermissionID = x,
                    RoleID = roleID
                }).ToList();

                _DB.Insertable(rolePermissions).ExecuteCommand();
            }

            if (entity.RoleDepartmentIDs.IsNotEmpty() && entity.RoleDepartmentIDs.Count > 0)
            {
                var roleDepartments = entity.RoleDepartmentIDs.Select(x => new RoleDepartment
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    DepartmentID = x,
                    RoleID = roleID
                }).ToList();

                _DB.Insertable(roleDepartments).ExecuteCommand();
            }

            return roleID;
        }
        #endregion

        #region Update
        public override int Update(Role entity)
        {
            int n = base.Update(entity);

            _DB.Deleteable<RolePermission>().Where(x => x.RoleID == entity.RoleID).ExecuteCommand();
            if (entity.PermissionIDs.IsNotEmpty() && entity.PermissionIDs.Count > 0)
            {
                var rolePermissions = entity.PermissionIDs.Select(x => new RolePermission
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    PermissionID = x,
                    RoleID = entity.RoleID
                }).ToList();

                _DB.Insertable(rolePermissions).ExecuteCommand();
            }

            _DB.Deleteable<RoleDepartment>().Where(x => x.RoleID == entity.RoleID).ExecuteCommand();
            if (entity.RoleDepartmentIDs.IsNotEmpty() && entity.RoleDepartmentIDs.Count > 0)
            {
                var roleDepartments = entity.RoleDepartmentIDs.Select(x => new RoleDepartment
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    DepartmentID = x,
                    RoleID = entity.RoleID
                }).ToList();

                _DB.Insertable(roleDepartments).ExecuteCommand();
            }

            return n;
        }
        #endregion

        #region GetByID
        public override Role GetByID(int id)
        {
            var entity = base.GetByID(id);
            entity.PermissionIDs = _DB.Queryable<RolePermission>().Where(x => x.RoleID == id).Select(x => x.PermissionID).ToList();
            entity.RoleDepartmentIDs = _DB.Queryable<RoleDepartment>().Where(x => x.RoleID == id).Select(x => x.DepartmentID).ToList();
            return entity;
        }
        #endregion

        #region 获取当前用户所有角色中为部门类型的角色IDs  GetCurrentUserRoleIDsByTypeWithDepartment
        public (bool, List<int>) GetCurrentUserRoleIDsByTypeWithDepartment()
        {
            int userID = _currentUser.CurrentUserInfo.UserID;

            //得到当前用户所有的角色IDs
            var roleIDs = _DB.Queryable<UserRole>().Where(x => x.UserID == userID).Select(x => x.RoleID).ToList();

            //得到角色的信息
            var roles = _DB.Queryable<Role>().Where(x => roleIDs.Contains(x.RoleID)).ToList();

            if (roles.Count(x => x.DataPermissionType == (int)Role_DataPermission.全部) == roles.Count)
            {
                return (true, null);
            }

            var resRoleIDs = roles.Where(x => x.DataPermissionType == (int)Role_DataPermission.部门).Select(x => x.RoleID).Distinct().ToList();
            return (false, resRoleIDs);
        }
        #endregion
    }
}
